var contextPath = "/cloud-favorite";
var common = {
    isNull: function (value) {
        return value == null || value == "" || value == undefined;
    },
    clearForm: function (ele) {
        $(":input", ele)
            .not(":button", ":reset", "hidden", "submit")
            .val("")
            .removeAttr("checked")
            .removeAttr("selected");
    },
    categoryCoverList: [],
    render: function (ele, userId) {
        if (common.categoryCoverList.length != 0) {
            common._renderCallback(ele, common.categoryCoverList);
        } else {
            $.ajax({
                url: contextPath+'/categoryCover',
                type: "get",
                data: {
                    pn: 1,
                    ps: 1000,
                    userId: userId
                },
                dataType: "json",
                success: function (resp) {
                    common.categoryCoverList = resp.items;
                    common._renderCallback(ele, common.categoryCoverList);
                }
            });
        }
    },
    _renderCallback: function (ele, items) {
        var str = " <option value=\"\">全部</option>";
        $(items).each(function (i) {
            str += "<option value=\"" + this.id + "\">" + this.coverName + "</option>";
        })
        $(ele).each(function () {
            $(this).html(str);
        });
    },
    getUrlParam: function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) {
            return decodeURIComponent(r[2]).replace(/\+/g, ' ');
        }
        return null; //返回参数值
    },
    coverPoper: function () {
        var ht =$("<div></div>").append($("#coverAddTip").html());
        $("#coverAddBtn").popover({
            content: ht,
            html: true,
            title: "添加分类",
            placement:"auto",
            trigger: 'manual',
        }).click(function (e) {
            $(this).popover("toggle");
        });
        $('body').on('click', function(event) {

            var target = $(event.target); // One jQuery object instead of 3
            // Compare length with an integer rather than with
            if (!target.hasClass('popover')
                && target.parent('.popover-content').length === 0
                && target.parent('.myPopover').length === 0
                && target.parent('.popover-title').length === 0
                && !target.hasClass('popover-field')
                && target.parent('.popover').length === 0 && target.attr("id") !== "coverAddBtn") {
                $('#coverAddBtn').popover('hide');
            }
        });
    }
}

var http = {
    del: function (path, id, callback) {
        $.ajax({
            url: path + '/' + id,
            type: 'delete',
            dataType: "json",
            success: function (data) {
                callback(data);
            }
        });
    },
    add: function (path, data, callback, failCallback) {
        $.ajax({
            url: path,
            type: "post",
            data: JSON.stringify(data),
            contentType: "application/json",
            dataType: "json",
            success: function (resp) {
                callback(resp);
            },
            error: function (resp) {
                failCallback(resp);
            }
        });
    },
    update: function (path, data, callback) {
        $.ajax({
            url: path,
            type: "put",
            data: JSON.stringify(data),
            contentType: "application/json",
            dataType: "json",
            success: function (resp) {
                callback(resp);
            }
        });
    }
}
var toastr;
$(function () {
    //设置显示配置
    toastr.options = {
        "closeButton": true,//是否显示关闭按钮
        "debug": false,//是否使用debug模式
        "positionClass": "toast-top-right",//弹出窗的位置
        "onclick": null,
        "showDuration": "300",//显示的动画时间
        "hideDuration": "1000",//消失的动画时间
        "timeOut": "5000",//展现时间
        "extendedTimeOut": "1000",//加长展示时间
        "showEasing": "swing",//显示时的动画缓冲方式
        "hideEasing": "linear",//消失时的动画缓冲方式
        "showMethod": "fadeIn",//显示时的动画方式
        "hideMethod": "fadeOut" //消失时的动画方式
    };
});

$.fn.loadJson = function (jsonValue) {
    var obj = this;
    $.each(jsonValue, function (name, ival) {
        var $oinput = obj.find(":input [name=" + name + "]");
        if ($oinput.attr("type") == "radio"
            || $oinput.attr("type") == "checkbox") {
            $oinput.each(function () {
                if (Object.prototype.toString.apply(ival) == '[object Array]') {//是复选框，并且是数组
                    for (var i = 0; i < ival.length; i++) {
                        if ($(this).val() == ival[i])
                            $(this).attr("checked", "checked");
                    }
                } else {
                    if ($(this).val() == ival)
                        $(this).attr("checked", "checked");
                }
            });
        } else if ($oinput.attr("type") == "textarea") {//多行文本框
            obj.find("[name=" + name + "]").html(ival);
        } else {
            obj.find("[name=" + name + "]").val(ival);
        }
    });
};

!function (a) {
    "use strict";
    a.fn.bootstrapTable.locales["zh-CN"] = {
        formatSearch: function () {
            return "回车搜索"
        }
    }, a.extend(a.fn.bootstrapTable.defaults, a.fn.bootstrapTable.locales["zh-CN"])
}(jQuery);


